package com.bj58.mis.apm.server.kylinreceive.service.impl;

import com.bj58.mis.apm.server.kylinreceive.model.KylinLog;
import com.bj58.mis.apm.server.kylinreceive.service.KylinSuggestService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: wangxw
 * @Date: 2018/7/23 11:10
 */
public class KylinSuggestByRedisServiceImpl implements KylinSuggestService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     *
     * @param logs
     */
    @Override
    public void updateQueryTimes(List<KylinLog> logs) {
        updateRedis(logs);
    }

    private void updateRedis(List<KylinLog> logs){
        //遍历日志
        logs.forEach(log -> {
            //1.更新修改的cubeid集合
            redisTemplate.opsForSet().add("kylog.cubes",log.getCubeId());
            //2.更新cubeid的计数
            increment("kylog.ts."+log.getCubeId());
            //3.
            String groupList = log.getGroupList();
            List<String> temp = new ArrayList<>();
            if(StringUtils.isNotEmpty(groupList)){
                String[] groups = groupList.split(",");
                temp.addAll(Arrays.asList(groups));
                for(String column : groups){
                    increment("kylog.ts.group."+log.getCubeId()+"."+column);
                    increment("kylog.ts.column."+log.getCubeId()+"."+column);
                }
            }
            String filterList = log.getFilterList();
            if(StringUtils.isNotEmpty(filterList)){
                String[] filters = filterList.split(",");
                for(String column : filters){
                    increment("kylog.ts.filter."+log.getCubeId()+"."+column);
                    if(temp.contains(column)){
                        continue;
                    }
                    temp.add(column);
                    increment("kylog.ts.column."+log.getCubeId()+"."+column);
                }
            }

            temp.sort((s1, s2) -> s1.compareTo(s2));
            increment("kylog.ts.groups."+log.getCubeId()+"."+StringUtils.join(temp, ","));
            increment("kylog.ts.cuboid."+log.getCubeId()+"."+log.getCuboid());

            setLastQueryTime(log, temp, log.getQueryTime().getTime());
        });

    }
    /**
     * 指定key加1
     * @param key
     */
    private void increment(String key){
        redisTemplate.boundValueOps(key).increment(1);
    }
    /**
     * 设置最后查询时间
     * @param log
     * @param columns
     * @param newTime
     */
    private void setLastQueryTime(KylinLog log, List<String> columns, Long newTime){
        String time = redisTemplate.opsForValue().get("kylog.lt.cube."+log.getCubeId());
        if(time == null || newTime.longValue() > Long.parseLong(time)) {
            redisTemplate.opsForValue().set("kylog.lt.cube."+ log.getCubeId(), newTime.toString());
            redisTemplate.opsForValue().set("kylog.lt.cuboid."+log.getCubeId()+"."+log.getCuboid(), newTime.toString());
            redisTemplate.opsForValue().set("kylog.lt.group."+log.getCubeId()+"."+StringUtils.join(columns, ","), newTime.toString());
            columns.forEach(col -> {
                redisTemplate.opsForValue().set("kylog.lt.column."+log.getCubeId()+"."+col, newTime.toString());
            });
        }
    }
}
